package com.blog.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.api.R;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.blog.domain.DTO.TagListDto;
 import com.blog.domain.ResponseResult;
 import com.blog.domain.entity.LoginUser;
 import com.blog.domain.entity.Tag;
 import com.blog.domain.vo.PageVo;
 import com.blog.domain.vo.TagVo;
 import com.blog.enums.AppHttpCodeEnum;
 import com.blog.exception.SystemException;
 import com.blog.mapper.TagMapper;
 import com.blog.service.TagService;
 import com.blog.utils.BeanCopyUtils;
 import com.blog.utils.SecurityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;

 import java.util.List;
 import java.util.Objects;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-09-11 09:35:00
 */
 @Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

	 @Autowired
	 private TagMapper tagMapper;

	/**标签分页查询
	 *
	 * @param pageNum
	 * @param pageSize
	 * @param tagListDto
	 * @return
	 */
	@Override
	 public ResponseResult<PageVo> pageTageList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
		 //分页查询
		 //分页条件
		 Page<Tag> page = new Page<>();
		 page.setCurrent(pageNum);
		 page.setSize(pageSize);
		 //查询条件

		 LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
		 wrapper.like(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
		 wrapper.like(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());

		 page(page,wrapper);
		 //封装数据返回
		 List<Tag> records = page.getRecords();
		 long total = page.getTotal();

		 PageVo pageVo = new PageVo(records,total);
		 return ResponseResult.okResult(pageVo);

	 }

	/**新增标签
	 *
	 * @param tagListDto
	 * @return
	 */
	@Override
	public ResponseResult addTag(TagListDto tagListDto) {
		//获取当前用户
		LoginUser loginUser = SecurityUtils.getLoginUser();
		/*//判断是否为空
		if (Objects.isNull(loginUser)){
			throw new SystemException(AppHttpCodeEnum.NEED_LOGIN);
		}*/

		Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
		save(tag);

		return ResponseResult.okResult();
	}

	/**删除标签
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public ResponseResult deleteTag(List<Long> ids) {
		removeByIds(ids);
		return ResponseResult.okResult();
	}

	/**获取标签详情
	 *
	 * @param id
	 * @return
	 */
	@Override
	public ResponseResult<TagListDto> getTagDetail(Long id) {
		Tag tag = getById(id);
		TagListDto tagListDto = BeanCopyUtils.copyBean(tag, TagListDto.class);
		return ResponseResult.okResult(tagListDto);
	}

	/**修改标签
	 *
	 * @return
	 */
	@Override
	public ResponseResult updateTag(TagListDto tagListDto) {
		Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
		updateById(tag);

		//tagMapper.updateTag(tagListDto);

		return ResponseResult.okResult();
	}

	/**查询所有标签
	 *
	 * @return
	 */
	@Override
	public ResponseResult<TagVo> selectAllTag() {
		List<Tag> list = list();
		List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);

		return ResponseResult.okResult(tagVos);
	}
}
